package applications.studentApplicationForm.ui.lib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

import applications.libraryForm.fc.api.LibraryForm;
import applications.studentApplicationForm.fc.api.StudentForm;

@Component(provides = {@Interface(name = "jPanelItf", signature = org.objectweb.fractal.swing.JPanelItf.class), @Interface(name = "jPanelAttributes", signature = org.objectweb.fractal.swing.JPanelAttributes.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})


public class StudentApplicationFormSemesterPanel extends JPanel  implements  java.util.Observer, org.objectweb.fractal.swing.JPanelItf, org.objectweb.fractal.swing.JPanelAttributes, ontocompo.api.UIContainerService, ontocompo.api.OntoCompoIntrospection {

	private String uid;
	private org.objectweb.fractal.api.Component c;
	private JTable table;


	@Requires(name = "studentformdirectoryservice")
	private applications.studentApplicationForm.fc.api.StudentFormDirectoryService studentformdirectoryservice;

	@Requires(name = "studentformdirectoryobservable")
	private ontocompo.api.Observable studentformdirectoryobservable;
	


	public StudentApplicationFormSemesterPanel() {
		super();
		this.uid = "applications.studentApplicationForm.ui.lib.StudentApplicationFormSemesterPanel";
		System.out.println(this.toString() + " created");
	}



	@Override
	public void init() {

		// TODO Auto-generated method stub

		this.studentformdirectoryobservable.addObserver(this);

		this.setLayout(new BorderLayout());
		this.setBackground(Color.white);


	}

	protected JLabel createTh(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setHorizontalAlignment(SwingConstants.CENTER);
		return result;
	}

	@Override
	public void update(Observable o, Object arg) {

		// TODO Auto-generated method stub

		StudentForm bk = this.studentformdirectoryservice.getStudentForm();

		if (bk == null){
			return;
		}
		else { 
			this.update(bk);
		}


	}

	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void update(Object o) {

		this.removeAll();
		this.setBackground(Color.white);

		JLabel lbl  = createTh("Semester");
		lbl.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);

		JPanel panelContenu = new JPanel(new BorderLayout());
		panelContenu.setBackground(Color.white);


		JPanel panelNorth = new JPanel();
		panelNorth.setBackground(Color.white);

		panelNorth.add(lbl);

		JPanel panelCenter = new JPanel();
		panelCenter.setBackground(Color.white);


		final String[] columnNames = this.studentformdirectoryservice.getSemesterTitles();

		// par defaut valeur == null
		if (this.studentformdirectoryservice.getYearValue() == null)
		{


			JScrollPane scrollPane = new JScrollPane();				
			panelCenter.add(scrollPane);

			panelContenu.add(panelNorth, BorderLayout.CENTER);
			
			this.add(panelContenu, BorderLayout.CENTER);

		}



		// on recupere l'annee lors du click
		else
		{
			final String[][] data = this.studentformdirectoryservice.getSemesterData(this.studentformdirectoryservice.getYearValue());

			// on utilise le tableModel pour empecher que l'utilisateur modifie les donnees lors du click
			TableModel dataModel = new AbstractTableModel() {
				public int getColumnCount() { return columnNames.length; }
				public int getRowCount() { return data.length;}
				public Object getValueAt(int row, int col) {
					return data[row][col];
				}
				public String getColumnName(int column) {return columnNames[column];}
				public Class getColumnClass(int col) {
					return getValueAt(0,col).getClass();
				}
				public void setValueAt(String aValue, int row, int column) {
					data[row][column] = aValue;
				}
			};

			table = new JTable(dataModel);
			table.setPreferredScrollableViewportSize(new Dimension(800, 60));
			table.setFillsViewportHeight(true);
			table.setForeground(Color.BLACK);
			table.setRowHeight(25);

			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setViewportView(table);



			panelCenter.add(scrollPane);
			panelContenu.add(panelNorth, BorderLayout.NORTH);
			panelContenu.add(panelCenter, BorderLayout.CENTER);


			this.add(panelContenu, BorderLayout.CENTER);
		}


		this.getParent().validate();  // pour rafraichir la page



	}

	public String toString() {
		return "StudentApplicationFormSemesterPanel";
	}




	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}





}



